<?php
/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/

?>
<?php
class Admin_UserController extends Zend_Controller_Action{
	public function init(){
		switch( $this->getRequest()->action ){
			case 'install':
			case 'login':
				break;
			default:
				if( !At_User::getInstance()->hasIdentity() ){
					$this->_forward( 'login' );
				}
		}

		$this->_helper->layout->setLayout('admin');
	}

	/**
	 * Defautl action
	 */
	public function indexAction(){
		if( At_User::getInstance()->hasIdentity() ){
			$this->_forward( 'profile' );
		}
		else{
			$this->_forward( 'login' );
		}
	}

	/**
	 * display a login screen if user isn't logged on, or redirect to user's profile page
	 */
	public function loginAction(){
		$form 	 = new Admin_Form_Login();
		$request = $this->getRequest();

		if( At_User::getInstance()->hasIdentity() ){
			$this->_redirect( '/admin/user/profile' );
		}
		else if( $request->isPost() && $form->isValid( $request->getPost() ) ){
			$auth  	= At_User::getInstance();
			$result = $auth->authenticate( new At_User_Adapter( $request->username, $request->password ) );

			if( $result->getCode() == 1 ){
				if( null !== $request->redirect && ! empty( $request->redirect ) ){
					$this->_redirect( $request->redirect );
				}

				$this->_redirect( '/admin/user/profile' );
			}
			else{
				$form->getElement( 'username' )->setErrors( $result->getMessages() );
			}
		}

		$this->view->form = $form;
	}

	/**
	 * destory current admin user's session
	 */
	public function logoutAction(){
		At_User::getInstance()->clearIdentity();
		$this->_redirect( '/admin/user/login' );
	}

	/**
	 * edit current admin user's details (e-mail, password)
	 */
	public function profileAction(){
		$user    = At_User::getInstance()->getIdentity();
//		$user 	 = new Default_Model_User( At_User::getInstance()->getIdentity()->id );
		$form 	 = new Admin_Form_User( $user );

		$reqeust = $this->getRequest();

		if( $this->getRequest()->isPost()  && $form->isValid( $this->getRequest()->getPost() ) ){
			//editing disabled now
		}

		$this->view->user = $user;
		$this->view->form = $form;

	}


	/**
	 * This is a rudimentary installer
	 */
	public function installAction(){
		// CHECK FOR ADMIN USERS IN DB
		$dbTable = new Zend_Db_Table('user_admin');
		$users = $dbTable->fetchAll( null, null, 1 );

		if( count( $users ) > 0 ){
			$this->_forward( 'login' );
		}

		// CAN DO INSTALL STUFF HERE
		$request = $this->getRequest();
		$form = new Admin_Form_User($this->getRequest());

		if( $request->isPost() && $form->isValid( $request->getPost() ) ){
			$values = $form->getValues();

			if( $values['password'] != $values['password_check'] ){
				$form->getElement('password')->addError( 'The passwords don\'t match' );
			}
			else{
				unset( $values['password_check'] );
				unset( $values['Save'] );
				$values['password'] = md5( $values['password'] );

				$dbTable->insert( $values );
				$this->_redirect( '/admin/user/login');
			}
		}

		$this->view->form = $form;
	}


	/**
	 * display a user-delete form
	 */
	public function deleteAction(){
		//@todo implement
	}

	/**
	 * list current admin users
	 */
	public function listAction(){
		//@todo implement
	}

	/**
	 * display a user-add form
	 */
	public function addAction(){
		//@todo implement
	}


	public function editAction(){
		//@todo implement
	}

}
